/**
 *
 */
package mainApp;

/**
 * @author Picto
 *
 */
import java.util.*;
import utils.*;
//TODO: use utils to accept input
public class HangmanApp {

    Scanner inputScan = new Scanner(System.in);
    Utils utility = new Utils();
    DataAdapter dataAdapter = new DataAdapter();
    Data data = new Data();

    public HangmanApp() {
        System.out.println("---------------------------------------");
        System.out.println("----------Welcome to Hangman!----------");
        System.out.println("---------------------------------------");
        data.setContinuePlay(true);

        data.setHighScores(dataAdapter.getHighScores());
        data.setHighScore(data.getHighScores().get(0));
    }

    public void runApp() {
        while (data.isContinuePlay() == true) {

            data.setWord(new Word());

            data.setAttempts(0);
            data.setGuessedCorrectly(false);

            char[] dst = new char[20];
            data.getWord().getValue().getChars(0, data.getWord().getWordLength(), dst, 0);
            

            
            for (int i = 0; i <= data.getWord().getWordLength() - 1; i++) {
                data.addInWordArray(dst[i]);
                data.addInWordComparisonArray(' ');
            }
            String inputVal;
            System.out.println("---------------------------------------");
            System.out.println("Enter L to guess a letter or W to guess at the word.");
            System.out.println("Word guesses cost 5 guesses!");
            System.out.println("Current score: " + getScore() + "     |     High score: " + getHighScore().getScore());
            while (getRemainingAttempts() > 0 & data.isGuessedCorrectly() == false) {
                System.out.println("Guesses remaining: " + getRemainingAttempts());
                System.out.println("---------------------------------------");
                System.out.println("Word:");
                System.out.println(utility.getWordString(data.getWord(), data.getWordComparisonArray()));

                //TODO: use utilities to test input

                System.out.println();
                System.out.println("Guess a letter or word?");
                inputVal = inputScan.next();
                if (inputVal.length() == 1) {
                    char select = inputVal.charAt(0);
                    System.out.println("What is your guess?");
                    String in = inputScan.next();
                    attempt(select, in);
                } else {
                    System.out.println("Please enter only L or W.");
                }
            }
            if (data.isGuessedCorrectly() == true) {
            } else {
            }
            System.out.println("---------------------------------------");
            System.out.println("Do you want to play again? (Y or N)");
            String in = inputScan.next();
            if (in.equals("N")) {
                data.setContinuePlay(false);
                setHighScore();
            }
            for (int i = 0; i < 10; ++i) {
                System.out.println();
            }
        }
    }

    public void attempt(char select, String in) {
        if (select == 'L') {
            if (in.length() == 1) {
                attemptLetter(in);
            } else {
                System.out.println("Please enter one character only.");
            }
        } else if (select == 'W') {
            attemptWord(in);
        } else {
            System.out.println("Invalid entry, please enter L or W.");
        }
    }

    public void attemptLetter(String in) {
        int counter = 0;
        for (int i = 0; i <= (data.getWord().getWordLength() - 1); i++) {
            if (data.getWordArray().get(i) == in.charAt(0)) {
                data.setInWordComparisonArray(i, in.charAt(0));
            } else {
                counter++;
            }
        }
        data.setWordComparisonArray(utility.truncateCharArrayList(data.getWordComparisonArray(), data.getWord().getWordLength()));
        if (counter == (data.getWord().getWordLength())) {
            System.out.println("Incorrect guess, please try again.");
        } else {
            System.out.println("There are " + ((data.getWord().getWordLength()) - counter) + " " + in + "s in the word.");
            if (data.getWordComparisonArray().containsAll(data.getWordArray())) {
                System.out.println("Congratulations the word was: " + data.getWord().getValue() + "!");
                correctGuess();
            }
        }
        data.addAttemptedLetter(in.charAt(0));
        data.incrementAttempt();
    }

    public void attemptWord(String in) {
        if (data.getWord().getValue().equals(in)) {
            System.out.println("Congratulations the word was: " + data.getWord().getValue() + "!");
            correctGuess();
        } else {
            System.out.println("Incorrect guess, please try again.");
            data.incrementAttemptWord();
        }
    }

    public int getRemainingAttempts() {
        int remainingAttempts = 10 - data.getAttempts();
        return remainingAttempts;
    }

    public String getWord() {
        return data.getWord().getValue();
    }

    public void correctGuess() {
        if (getDifficulty().equals(Difficulty.EASY)) {
            data.incrementScore(1);
        } else if (getDifficulty().equals(Difficulty.MEDIUM)) {
            data.incrementScore(2);
        } else {
            data.incrementScore(3);
        }
        data.setGuessedCorrectly(true);
    }

    public int getScore() {
        return data.getScore();
    }

    public void setHighScore() {
        System.out.println("Please enter a username.");
        dataAdapter.addHighScore(inputScan.next(), getScore());
    }

    public HighScore getHighScore() {
        return data.getHighScore();
    }

    public String getUser() {
        System.out.println("Please enter username");
        return inputScan.next();
    }

    public void loadHighScores() {
        data.setHighScores(dataAdapter.getHighScores());
    }

    public Difficulty getDifficulty() {
        if (data.getWord().getWordLength() < 5) {
            return Difficulty.EASY;
        } else if (data.getWord().getWordLength() > 8) {
            return Difficulty.HARD;
        } else if (data.getWord().getWordLength() >= 5 & data.getWord().getWordLength() <= 8) {
            return Difficulty.MEDIUM;
        } else {
            return Difficulty.ERROR_OUT_OF_RANGE;
        }
    }
}
